package io.github.codelifeleno.jdbrelease.model;

import cn.hutool.core.io.resource.Resource;
import io.github.codelifeleno.jdbrelease.config.DBScriptConfig;
import org.apache.commons.lang3.builder.EqualsBuilder;
import org.apache.commons.lang3.builder.HashCodeBuilder;
import org.apache.commons.lang3.builder.ToStringBuilder;

import java.io.File;

/**
 * 脚本执行过程对象
 */
public class DBScriptRunModel {

    /**
     * 执行的脚本文件
     */
    private final Resource scriptFilePath;
    /**
     * 执行的脚本配置
     */
    private final DBScriptConfig scriptConfig;
    /**
     * 脚本执行状态
     */
    private boolean runStatus = Boolean.TRUE;

    private boolean rollbackStatus = Boolean.FALSE;
    /**
     * 执行错误的异常脚本
     */
    private final String scriptString;
    /**
     * 执行过程目录
     */
    private final File thisLogFile;

    @Override
    public String toString() {
        return new ToStringBuilder(this)
                .append("scriptFilePath", scriptFilePath)
                .append("scriptConfig", scriptConfig)
                .append("runStatus", runStatus)
                .append("rollbackStatus", rollbackStatus)
                .append("scriptString", scriptString)
                .append("thisLogFile", thisLogFile)
                .toString();
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;

        if (o == null || getClass() != o.getClass()) return false;

        DBScriptRunModel that = (DBScriptRunModel) o;

        return new EqualsBuilder().append(isRunStatus(), that.isRunStatus()).append(isRollbackStatus(), that.isRollbackStatus()).append(getScriptFilePath(), that.getScriptFilePath()).append(getScriptConfig(), that.getScriptConfig()).append(getScriptString(), that.getScriptString()).append(getThisLogFile(), that.getThisLogFile()).isEquals();
    }

    @Override
    public int hashCode() {
        return new HashCodeBuilder(17, 37).append(getScriptFilePath()).append(getScriptConfig()).append(isRunStatus()).append(isRollbackStatus()).append(getScriptString()).append(getThisLogFile()).toHashCode();
    }

    public Resource getScriptFilePath() {
        return scriptFilePath;
    }

    public DBScriptConfig getScriptConfig() {
        return scriptConfig;
    }

    public boolean isRunStatus() {
        return runStatus;
    }

    public void setRunStatus(boolean runStatus) {
        this.runStatus = runStatus;
    }

    public boolean isRollbackStatus() {
        return rollbackStatus;
    }

    public void setRollbackStatus(boolean rollbackStatus) {
        this.rollbackStatus = rollbackStatus;
    }

    public String getScriptString() {
        return scriptString;
    }

    public File getThisLogFile() {
        return thisLogFile;
    }

    public DBScriptRunModel(Resource scriptFilePath, DBScriptConfig scriptConfig, String scriptString, File thisLogFile) {
        this.scriptFilePath = scriptFilePath;
        this.scriptConfig = scriptConfig;
        this.scriptString = scriptString;
        this.thisLogFile = thisLogFile;
    }
}
